import os
#import logging
import config
import sys
import parser
import importlib
import importlib.util

#logging.basicConfig(level=logging.INFO, filename='./report.log', filemode='w')
#logger = logging.getLogger()

succcess_list = []
failed_list = []

def generate_report():
    file_name = './report.log'
    with open(file_name, 'w') as f:
        f.write('-------------------success test case-------------------')
        f.write('\n')

        n = len(succcess_list)
        f.write('Count: ' + str(n))
        f.write('\n')

        f.write('Case: \n')

        for s in succcess_list:
            print('success:', s)
            f.write('----' + s)
            f.write('\n')

        f.write('-------------------failed test case-------------------')
        f.write('\n')

        n = len(failed_list)
        f.write('Count: ' + str(n))
        f.write('\n')

        f.write('Case: \n')

        for failed in failed_list:
            print('failed:', failed)
            f.write('----' + failed['case'])
            f.write(', failed phase: ' + failed['phase'])
            f.write('\n')    

def check_module(module_name):
    """
    Checks if module can be imported without actually
    importing it
    """
    module_spec = importlib.util.find_spec(module_name)
    if module_spec is None:
        print("Module: {} not found".format(module_name))
        return None
    else:
        print("Module: {} can be imported".format(module_name))
        return module_spec

if __name__ == "__main__":
    parser.init()

    case_list = []
    filter_list = ['__init__.py', 'base.py']

    for file in os.listdir('./case'):
        file_path = os.path.join('./case', file)
        if os.path.isdir(file_path) == False and file not in filter_list:
            case_list.append(file)

    for case in case_list:
        print('case:', case)
        case = case.split('.')[-2]
        target_module = 'case.' + case
        module_find = check_module(target_module)
        print('----get module: ', module_find)

        if module_find != None :
            #if case == 'Darknet2ONNX':
            if True:    
                module = importlib.import_module(target_module)
                cls = getattr(module, case)
                obj = cls(succcess_list, failed_list)
                obj.test()
                obj.clear()  

    generate_report()